import os,tempfile,shutil,commands
import pickle
import sys
import time
from crvclient import *

if __name__ == '__main__':

    # Here we do:
    # 1. setup the certs for use with curl
    # 2. setup the puppet ssldir and put in the certs for puppet access
    # 3. add to /etc/hosts puppetsrv -> serverhostname
    # 4. make puppet var dir

    if len(sys.argv) != 4:
        raise RuntimeError("Usage: setupCRVClient.py values.pickle crvclient.pickle workdir")

    valuesfile=sys.argv[1]
    crvclientfile=sys.argv[2]
    workdir=sys.argv[3]

    inf=open(valuesfile,"r")
    #Read in values first
    vmid=pickle.load(inf)
    
    #the http://xxxx the cloudcrv status controller
    serverstr=pickle.load(inf)

    #for the puppet server
    serverhostname=pickle.load(inf)

    #certname, expect the files crvclient/certname.crt, prk, puk and pass.txt
    certname=pickle.load(inf)
    inf.close()

    #basic stuff
    
    clientconf=CRVClientConfig()
    clientconf.cloudcrvsrv=serverhostname
    clientconf.statusurl=serverstr
    clientconf.vmid=vmid
    clientconf.certname=certname
    clientconf.cacertpem="%s/ca.crt"%workdir
    clientconf.workdir=workdir

    #Curl certs
    inf=open("%s/%s.crt"%(workdir,certname),"r")
    pemstr=inf.read()
    inf.close()
    
    inf=open("%s/%s.prk"%(workdir,certname),"r")
    pemstr=inf.read()+pemstr
    inf.close()

    clientconf.curlpem="%s/curlcert.pem"%workdir
    ouf=open(clientconf.curlpem,"w")
    ouf.write(pemstr)
    ouf.close()

    inf=open("%s/%s.pass"%(workdir,certname),"r")
    clientconf.keypass=inf.read()
    inf.close()

    clientconf.curlopt="-E %s:%s --cacert %s"%(clientconf.curlpem, clientconf.keypass, clientconf.cacertpem)

    #puppet certs
    clientconf.puppetserver=clientconf.cloudcrvsrv
    clientconf.puppetservercertname="puppetsrv"
    clientconf.puppetcertname=certname
    clientconf.puppetvardir="/var/puppet/varpuppet"
    clientconf.puppetssldir="%s/ssl"%workdir
    clientconf.puppetpassfile="%s/%s.pass"%(clientconf.puppetssldir, certname)

    #files needed are:
    #ca cert: /var/puppet/ssl/certs/ca.pem
    #client cert: /var/puppet/ssl/certs/puppetclient.pem
    #client private key: /var/puppet/ssl/private_keys/puppetclient.pem
    #password file: /var/puppet/ssl/pass.txt

    os.makedirs("%s/certs"%clientconf.puppetssldir)
    os.makedirs("%s/private_keys"%clientconf.puppetssldir)

    shutil.copyfile(clientconf.cacertpem, "%s/certs/ca.pem"%clientconf.puppetssldir)
    shutil.copyfile("%s/%s.crt"%(workdir,certname), "%s/certs/%s.pem"%(clientconf.puppetssldir, certname))
    shutil.copyfile("%s/%s.prk"%(workdir,certname), "%s/private_keys/%s.pem"%(clientconf.puppetssldir, certname))
    
    shutil.copyfile("%s/%s.pass"%(workdir,certname),"%s/%s.pass"%(clientconf.puppetssldir, certname))
    
    #add to /etc/hosts
    ouf=open("/etc/hosts","a")
    ouf.write("\n")
    ouf.write("%s %s\n"%(clientconf.puppetserver, clientconf.puppetservercertname))
    ouf.write("%s %s\n"%(clientconf.puppetserver, "cloudcrvsrv"))
    ouf.close()

    #make puppet var dir
    os.makedirs(clientconf.puppetvardir)
    
    ouf=open(crvclientfile,"w")
    pickle.dump(clientconf,ouf)
    ouf.close()
